home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / ip / dns / rovers / ProblemManager / ProblemFileio.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-24  |  5.0 KB  |  155 lines

  1. /*
  2.     ProblemFileio.c - All I/O with PROBLEM FILE uses these routines
  3.  */
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include "ProblemFileio.h"
  7. #include "../Ctools/ctools.h"
  8. #define FALSE 0
  9. #define TRUE 1
  10.  
  11. int In_Critical_Section=0;
  12.  
  13. void PrintProblems(NumProblems,Problem)
  14. int NumProblems;
  15. struct ProblemType *Problem;
  16. {
  17. int i;
  18. char buffer[100];
  19.  
  20.    for(i=0; i<NumProblems; i++) {
  21.     if ( Problem[i].Name[0]!='\0')
  22.               printf("TimeStamp=%s Day: %s Time:%s Node: %s [%s] Failed %s Test StatusLine: %s \n",
  23.             ltoa(Problem[i].TimeStamp,buffer),
  24.                    _Day(Problem[i].TimeStamp),_Time(Problem[i].TimeStamp),Problem[i].Name,
  25.             Problem[i].UniqueID,Problem[i].TestName,Problem[i].StatusLine);
  26.    }
  27. }
  28.  
  29. /***************************************************************************
  30.  * Problem_Exists:  Returns index of Problem if it exists and -1 otherwise *
  31.  ***************************************************************************/
  32. int Problem_Exists(UniqueID,Service,Problem,NumProblems)
  33. char *UniqueID;
  34. char *Service;
  35. struct ProblemType *Problem;
  36. int NumProblems;
  37. {
  38. int i;
  39.  
  40.    for (i=0; i<NumProblems ; i++) {
  41.        if (strcmp(Problem[i].UniqueID,UniqueID)==0)
  42.           if (strcmp(Problem[i].TestName,Service)==0)
  43.                         return(i);
  44.    }
  45.    return(-1);
  46. }
  47.  
  48.  
  49. /*****************************************************************************
  50.  * ReadProblemFile:  Read the Problems from file into our internal format.   *
  51.  *****************************************************************************/
  52. int ReadProblemFile(Problem,file) /* Read and Store hostfile - return # read */
  53. struct ProblemType *Problem;      /* Network Entities Structure Array        */
  54. char *file;                       /* Filename to read these from             */
  55. {
  56. FILE *stream;
  57. char buffer[100],*p;
  58. int Error=FALSE;
  59. int i,line;
  60.  
  61.     In_Critical_Section=1;
  62.    if ( !lock(file) )
  63.     panic("Read_ProblemFile: PROBLEM.FILE LOCKED");
  64.  
  65.    if ((stream=fopen(file,"r")) == NULL)
  66.       syserr("fopen PROBLEM FILE");
  67.    for(i=0,line=0;((fgets(buffer,100,stream)!= NULL)&&(i<MAXPROBLEMS));line++){
  68.       if (Error==TRUE) {
  69.       /*   printf("Error in line %d of Problem File - IGNORED\n",line); */
  70.       }
  71.       Error=TRUE;       /* Assume a bad line */
  72.       if (buffer[0]=='#') { Error=FALSE; continue; } /* COMMENT */
  73.  
  74.       if ( (p=strtok(buffer,DELIMITERS)) == NULL ) continue;
  75.     Problem[i].TimeStamp = atol( p );
  76.  
  77.       if ( (p=strtok(NULL,DELIMITERS)) == NULL ) continue;
  78.       strncpy(Problem[i].Name,p,MAXNODENAME);      /* Pull off Node name */
  79.       Problem[i].Name[MAXNODENAME-1]='\0';         /* Null Terminate!    */
  80.  
  81.       if ( (p=strtok(NULL,DELIMITERS)) == NULL ) continue;
  82.       strncpy(Problem[i].UniqueID,p,MAXUNIQUEID);       /* Pull off Node UniqueIDess */
  83.       Problem[i].UniqueID[MAXUNIQUEID-1]='\0';          /* Null Terminate! */
  84.  
  85.       if ( (p=strtok(NULL,DELIMITERS)) == NULL ) continue;
  86.       strncpy(Problem[i].TestName,p,MAXTESTNAME); /* Pull off Node UniqueIDess */
  87.       Problem[i].TestName[MAXTESTNAME-1]='\0';    /* Null Terminate! */
  88.  
  89.       Error=FALSE;                              /* Pull off NetMgmtProtocols */
  90.       Problem[i].StatusLine[0]='\0';
  91.       while( (p=strtok(NULL,DELIMITERS)) != NULL)  {
  92.          strncat(Problem[i].StatusLine,p,MAXSTATUSLINE-strlen(Problem[i].StatusLine));
  93.          strncat(Problem[i].StatusLine," ",MAXSTATUSLINE-strlen(Problem[i].StatusLine));
  94.       }
  95.       Problem[i].StatusLine[MAXSTATUSLINE-1]='\0';
  96.  
  97.       i++;              /** Yea! We have a valid line **/
  98.    }
  99.    fclose(stream);
  100.    Problem[i].Name[0]='\0';
  101.    unlock(file);
  102.    In_Critical_Section=0;
  103.    return(i);
  104. }
  105.  
  106. #define _1_GREATER_2 1
  107. #define _1_LESSTHAN_2 -1
  108.  
  109. int CompareRoutine( p1, p2 )
  110. struct ProblemType *p1,*p2;
  111. {
  112.     if (( p1->StatusLine[0] == '\0' ) && ( p2->StatusLine[0] != '\0' ))
  113.         return( _1_LESSTHAN_2 );
  114.     if (( p1->StatusLine[0] != '\0' ) && ( p2->StatusLine[0] == '\0' ))
  115.         return( _1_GREATER_2 );
  116.  
  117.     /* BOTH ARE COMMENTED OR NOT COMMENTED - Sort by Time */
  118.     return( p2->TimeStamp - p1->TimeStamp );
  119. }
  120.  
  121. /*****************************************************************************
  122.  * WriteProblemFile:  Write our problems to disk.                            *
  123.  *****************************************************************************/
  124. int WriteProblemFile(Problem,Problem_File,NumProblems)
  125. struct ProblemType *Problem;
  126. char *Problem_File;
  127. int NumProblems;
  128. {
  129. FILE *stream;
  130. int i;
  131. char buffer[20];
  132.  
  133.    In_Critical_Section=1;
  134.    if ( ! lock(Problem_File) )
  135.     panic("WriteProblemFile: PROBLEM.FILE LOCKED ");
  136.  
  137.    if ((stream=fopen(Problem_File,"w"))==NULL) {
  138.       perror("PROBLEM FILE NOT FOUND! Error.");
  139.       exit(1);
  140.    }
  141.  
  142.    qsort( Problem, NumProblems, sizeof(struct ProblemType), CompareRoutine );
  143.  
  144.    for(i=0; i<NumProblems; i++) {
  145.       if (Problem[i].Name[0]!='\0')
  146.              fprintf(stream,"%s %s %s %s %s\n",
  147.                 ltoa(Problem[i].TimeStamp,buffer),Problem[i].Name,
  148.                 Problem[i].UniqueID,Problem[i].TestName,Problem[i].StatusLine);
  149.    }
  150.    fclose(stream);
  151.    unlock(Problem_File);
  152.    In_Critical_Section=0;
  153. }
  154.  
  155.